package com.flyaudio.xiaopengautopilot.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
 * Created by Administrator on 2018/9/29.
 */
public class FileTool
{
    private static String TAG = "FileTool";
    private FileTool(){
    }
    public static boolean isExistFile(String Path)
    {
        File file = new File(Path);
        return file.exists();
    }
    public static boolean delete(String Path)
    {
        File file = new File(Path);
        file.setWritable(true,true);
        boolean ret = file.delete();
        if(!ret)
            L.e(TAG,"delete "+Path+" fail");
        return ret;
    }
    public static void copyFileUsingFileChannels(String source, String dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        File SourceFile = new File(source);
        File DestFile = new File(dest);
        if(!DestFile.exists()){
            DestFile.createNewFile();
        }
        L.d(TAG,"canWrite "+DestFile.canWrite());
        try{
            inputChannel = new FileInputStream(SourceFile).getChannel();
            outputChannel = new FileOutputStream(DestFile).getChannel();
            if(inputChannel!=null&&outputChannel!=null)
            {
                outputChannel.transferFrom(inputChannel,0,inputChannel.size());
            }
            else
                L.e(TAG,"copyFileUsingFileChannels err");
        }
        finally {
            if (inputChannel != null)
                inputChannel.close();
            if (outputChannel != null)
                outputChannel.close();
        }
    }
}